package com.ch.boundarycallbackdemo.db

import androidx.annotation.UiThread
import androidx.paging.PagedList
import com.ch.boundarycallbackdemo.bean.Data
import com.ch.boundarycallbackdemo.bean.StateBean
import com.ch.boundarycallbackdemo.viewmodel.NewsViewModel
import com.ch.boundarycallbackdemo.repository.NewsRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class NewsBoundCallBack(newsViewModel: NewsViewModel) : PagedList.BoundaryCallback<Data>() {
    private var curNumber = 0;

    private var viewModel: NewsViewModel? = null

    init {
        viewModel = newsViewModel
    }

    companion object {
        const val PAGE_SIZE = 16
    }

    //加载首页数据
    override fun onZeroItemsLoaded() {
        super.onZeroItemsLoaded()
        curNumber = 1

        setBeanState();


    }

    //加载下一页数据
    override fun onItemAtEndLoaded(itemAtEnd: Data) {
        super.onItemAtEndLoaded(itemAtEnd)
        curNumber++
        setBeanState();
    }

    //加载上一页数据
    override fun onItemAtFrontLoaded(itemAtFront: Data) {
        super.onItemAtFrontLoaded(itemAtFront)
        curNumber--
        if (curNumber < 1) {
            curNumber = 1
        }
        setBeanState();
    }

    private fun setBeanState() {
        CoroutineScope(Dispatchers.IO).launch {
            var result = NewsRepository.getNewsList(curNumber)
            if (result.error_code != 0) {
                val stateBean = StateBean(result.error_code, result.reason)
                viewModel?.setStateBean(stateBean)
            } else {
                val result = result?.result
                result?.let {
                    NewsDataBase.INSTALL.getNewsDao().insertNewsList(it.data)
                }
            }
        }
    }

//    private fun getNewsData(page: Int) {
//        var map = HashMap<String, String>()
//        map["key"] = "192a2e7da8467d5fd853b29677b373f9"
//        map["page"] = "$page"
//        map["page_size"] = "$PAGE_SIZE"
//
//        RetrofitManager.createServiceApi(NewsApi::class.java).getNews(map)
//            .enqueue(object : Callback<NewsBean> {
//                override fun onResponse(call: Call<NewsBean>, response: Response<NewsBean>) {
//                    val newsBean = response.body();
//                    if (newsBean?.error_code != 0) {
//                        newsBean?.let {
//                            val stateBean = StateBean(newsBean.error_code, newsBean.reason)
//                           // viewModel?.stateBean?.value = stateBean
//                            viewModel?.setStateBean(stateBean)
//                        }
//                    }
//                    val result = newsBean?.result
//                    result?.let {
//                        AsyncTask.execute(Runnable {
//                            NewsDataBase.INSTALL.getNewsDao().insertNewsList(it.data)
//                        })
//                    }
//                }
//
//                override fun onFailure(call: Call<NewsBean>, t: Throwable) {
//                    Log.e("Hogan", t.printStackTrace().toString())
//                }
//
//            })
//    }
}